package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.provider.mapper.DeptMapper;
import com.weixing.mall.provider.model.domain.Dept;
import com.weixing.mall.provider.model.dto.sys.SysDeptDto;
import com.weixing.mall.provider.model.vo.sys.SysDeptCascaderVo;
import com.weixing.mall.provider.model.vo.sys.SysDeptTreeVo;
import com.weixing.mall.provider.service.IDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统模块-组织结构表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Override
    public List<SysDeptTreeVo> selectTree() {
        //查询出所有的dept
        List<SysDeptTreeVo> srcDeptTreeVo = baseMapper.selectAllList();
        List<SysDeptTreeVo> distDeptTreeVo = Lists.newArrayList();
        //获取根目录
        if (!srcDeptTreeVo.isEmpty()) {
            // 根节点 为固定的id为1的记录
            Dept rootDept = getById(1);
            SysDeptTreeVo rootDeptTreeVo = new SysDeptTreeVo();
            BeanUtils.copyProperties(rootDept, rootDeptTreeVo);
            rootDeptTreeVo.setIsEnable(IsEnableEnum.ENABLE);
            fillSysDeptTreeVo(distDeptTreeVo, srcDeptTreeVo, rootDeptTreeVo);
            distDeptTreeVo.add(rootDeptTreeVo);
        }
        return distDeptTreeVo;
    }

    @Override
    public void save(SysDeptDto sysDeptDto) {
        Dept sysDept = new Dept();
        BeanUtils.copyProperties(sysDeptDto, sysDept);
        sysDept.setIsEnable(sysDeptDto.getIsEnable().getValue());
        if (null == sysDeptDto.getId()) {
            save(sysDept);
            Long id = sysDept.getId();
            sysDept.setDeptPath(sysDeptDto.getDeptPath() + "|" + id);
            updateById(sysDept);
        } else {
            sysDept.setUpdatedTime(new Date());
            updateById(sysDept);
        }
    }

    @Override
    public List<SysDeptCascaderVo> selectSysDeptCascaderVo() {
        //查询出所有的dept
        List<SysDeptCascaderVo> srcDeptVo = baseMapper.selectSysDeptCascaderVo();
        List<SysDeptCascaderVo> distDeptVo = Lists.newArrayList();
        //获取根目录
        if (!srcDeptVo.isEmpty()) {
            SysDeptCascaderVo rootDeptVo = srcDeptVo.get(0);
            fillSysDeptCascaderVo(distDeptVo, srcDeptVo, rootDeptVo);
            distDeptVo.add(rootDeptVo);
        }

        return distDeptVo;
    }


    /**
     * Description: 递归封装菜单Tree结构 <br/>
     *
     * @param src  源集合
     * @param root 跟目录
     * @param:dist 目标集合
     * @return:
     * @Author: kfc
     * @Date: 2019/5/17 10:26
     */
    private void fillSysDeptTreeVo(List<SysDeptTreeVo> dist, List<SysDeptTreeVo> src, SysDeptTreeVo root) {
        List<SysDeptTreeVo> menuTreeVoList = src.stream().filter((SysDeptTreeVo m1) -> m1.getParentId().longValue() == root.getId().longValue()).collect(Collectors.toList());
        if (!menuTreeVoList.isEmpty()) {
            menuTreeVoList.sort(new Comparator<SysDeptTreeVo>() {
                @Override
                public int compare(SysDeptTreeVo o1, SysDeptTreeVo o2) {
                    return o1.getOrderBy() - o2.getOrderBy();
                }
            });
            root.setChildren(menuTreeVoList);
//            root.setHasChildren(true);
            src.removeAll(menuTreeVoList);
            for (SysDeptTreeVo m : menuTreeVoList) {
                fillSysDeptTreeVo(dist, src, m);
            }
        }
    }


    /**
     * Description: 递归封装菜单Tree结构 <br/>
     *
     * @param src  源集合
     * @param root 跟目录
     * @param:dist 目标集合
     * @return:
     * @Author: kfc
     * @Date: 2019/5/17 10:26
     */
    private void fillSysDeptCascaderVo(List<SysDeptCascaderVo> dist, List<SysDeptCascaderVo> src, SysDeptCascaderVo root) {
        List<SysDeptCascaderVo> menuTreeVoList = src.stream().filter((SysDeptCascaderVo m1) -> m1.getParentId() == root.getValue()).collect(Collectors.toList());
        if (!menuTreeVoList.isEmpty()) {
            menuTreeVoList.sort(new Comparator<SysDeptCascaderVo>() {
                @Override
                public int compare(SysDeptCascaderVo o1, SysDeptCascaderVo o2) {
                    return o1.getOrderBy() - o2.getOrderBy();
                }
            });
            root.setChildren(menuTreeVoList);
            src.removeAll(menuTreeVoList);
            for (SysDeptCascaderVo m : menuTreeVoList) {
                fillSysDeptCascaderVo(dist, src, m);
            }
        }
    }
}
